import axios from 'axios'
import { Message } from 'element-ui'
import cookie from 'js-cookie'


// 创建axios实列
const service = axios.create({
  baseURL: 'http://localhost:8100',
  timeout: 20000
})

service.interceptors.request.use(request => {
  if (cookie.get('guli_token')) {
    request.headers['token'] = cookie.get('guli_token');
  }
  return request;
}, err => {
  return Promise.reject((err));
})

service.interceptors.response.use(response => {
  const res = response.data
  if (res.code !== 200) {
    Message({
      message: res.message,
      type: 'error',
      duration: 5 * 1000
    })
   return Promise.reject('error')
  } else {
    return response
  }
})

export default service
